package com.szewec.ps.ui.tabbar;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.szewec.ps.R;
import com.szewec.ps.base.CommonDialog;
import com.szewec.ps.databinding.FragmentMineBinding;
import com.szewec.ps.ui.appauth.AppAuthActivity;
import com.szewec.ps.ui.login.LoginActivity;
import com.szewec.ps.util.LogUtil;
import com.szewec.ps.util.PreferincesContext;
import com.szewec.ps.util.SharedPreferencesUtil;

import butterknife.ButterKnife;
import butterknife.OnClick;

//我的fragment
public class MineFragment extends Fragment {

    private FragmentMineBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 1、对布局需要绑定的内容进行加载
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_mine, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);
        String companyName = (String) SharedPreferencesUtil.getData(PreferincesContext.USER_COMPANY_NAME,"");
        String userName = (String) SharedPreferencesUtil.getData(PreferincesContext.USER_SHOW_NAME,"");
        binding.tvUserName.setText(userName);
        binding.tvCompanyName.setText(companyName);

        return inflate;
    }

    @OnClick({R.id.sign_out_btn,R.id.rl_add_role_user,R.id.rl_add_teamer})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sign_out_btn:
                //退出登录
                logout();
                break;
            case R.id.rl_add_role_user:
                //应用授权
                Intent intent = new Intent(getActivity(), AppAuthActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_add_teamer:
                //添加项目成员
                break;
        }
    }

    private void logout(){
        final CommonDialog commonDialog = new CommonDialog(getActivity());
        commonDialog.setTitle("提示");
        commonDialog.setContent("确定退出登录?");
        commonDialog.setOnEnsureClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击“确认”后的操作
                SharedPreferencesUtil.putData(PreferincesContext.PROJECT_ID,"");
                SharedPreferencesUtil.putData(PreferincesContext.STAGE_ID,"");
                SharedPreferencesUtil.putData(PreferincesContext.USERNAME,"");
                SharedPreferencesUtil.putData(PreferincesContext.PASSWORD,"");
                SharedPreferencesUtil.putData(PreferincesContext.TOKEN,"");
                SharedPreferencesUtil.putData(PreferincesContext.USER_SHOW_NAME,"");
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                commonDialog.dismiss();
            }
        });
        commonDialog.setOnCancelClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commonDialog.dismiss();
            }
        });
        commonDialog.show();
    }
}
